Page({
    data: {
        banners: {},  //banners 列表
        isType: 0, //图书类型
        booksList: ["aa","bb","cc"],//图书列表
        page:1, //表示当前页数
        pageA:1, //热门的当前页数
        pageB:1 //最新的当前页数

    },
    onLoad: function (options) {
        //初始化 获取数据
        // 从后台获取轮播图数据
        this.setBannersData();
        this.setbooksListData(0);

    },
    // 上拉触底
    onReachBottom(){
      // console.log("上拉触底")
      //获取向更多的相同种类的图书信息
      // 1.当前类型 2.表示当前页数
      let typeId = this.data.isType;
      let pagea = this.data.pageA;
      let pageb = this.data.pageB;
      if(typeId==0){
        this.setData({
          pageA:++pagea
        })
      this.loadMoreBooksList(typeId,this.data.pageA);
      }else{
        this.setData({
          pageB:++pageb
        })
      this.loadMoreBooksList(typeId,this.data.pageB);
      }
    },
    setBannersData() {
        let that = this
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?state=2`,
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: function (res) {
                if (res.statusCode == 200) {
                    that.setData({
                        banners: res.data.list
                    })
                    console.log(res.data.list)
                    //将轮播图数据保存起来，保存到banners
                }
            },
            fail: function (res) {},
            complete: function (res) {}
        })
    },
    setbooksListData(typeId) {
        let that = this
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?state=${typeId}`,
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: function (res) {
                if (res.statusCode == 200) {
                    that.setData({
                        booksList: res.data.list
                    })
                    console.log(res.data.list)
                    //将轮播图数据保存起来，保存到banners
                }
            },
            fail: function (res) {},
            complete: function (res) {}
        })
    },
    toggletype(e) {
        this.setData({
            isType: e.currentTarget.dataset.typeId,
            pageA:1,
            pageB:1
        })
        //获取与类型相对应的图书数据
        this.setbooksListData(e.currentTarget.dataset.typeId)
    },
    // 加载更多的图书
    loadMoreBooksList(typeId,page){
        wx.showLoading({
            title: 'title',
        })
      //拿到相对应的类型和页数之后
      console.log(typeId,page)
      let that = this
      wx.request({
          url: `http://192.168.0.82:8080/bookshoop/books/query?state=${typeId}&&page=${page}`,
          data: {},
          header: {
              "content-type": "application/json"
          },
          success: function (res) {
              if (res.statusCode == 200) {
                  setTimeout(()=>{
                       that.setData({
                      booksList: res.data.list.concat(that.data.booksList)
                  })
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  },500)
                //将之前的数据和新的数据合并
                 
                  console.log(res.data.list)
              }
          },
          fail: function (res) {},
          complete: function (res) {}
      })
    },
    //实现搜索更多图书
    searchBar(e){
        //拿到想要搜索图书的title
        console.log(e.detail.value.val)
        // 当前的类型
        console.log(this.data.isType)
        let that = this
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?isType=${that.data.isType}&&title=${e.detail.value.val}&&rows=100`,
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: function (res) {
                if (res.statusCode == 200) {
                    setTimeout(()=>{
                         that.setData({
                        booksList: res.data.list
                    })
                    wx.hideLoading({
                      success: (res) => {},
                    })
                    },500)
                  //将之前的数据和新的数据合并
                   
                    console.log(res.data.list)
                }
            },
            fail: function (res) {},
            complete: function (res) {}
        })

    },
   
})